package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /*
    * 查询所有的角色
    * */
    @GetMapping("all")
    public Result selectRoleAll(){
        try {
            List<Role> roleList = this.roleService.selectRoleAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*
     *根据id查询数据
     * */
    @GetMapping(path = "/{id}")
    public Result selectByIdRole(@PathVariable("id") Integer id){
        try {
            Role role = this.roleService.selectByIdRole(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*
     * 查询角色关联的菜单ID
     * */
    @GetMapping(path = "/{id}/menu")
    public Result selectByIdRoleMenu(@PathVariable("id") Integer id){
        try {
            Integer[] menuIds = this.roleService.selectByIdRoleMenu(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*
     * 查询角色关联的权限ID
     * */
    @GetMapping(path = "/{id}/permission")
    public Result selectByIdRolePermission(@PathVariable("id") Integer id){
        try {
            Integer[] permissionIds = this.roleService.selectByIdRolePermission(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*
    * 分页（关键字）查询
    * */
    @GetMapping("page")
    public Result selectByNameRole(Integer currentPage, Integer pageSize, Long total,String queryString){
        try {
            PageResult pageResult = this.roleService.selectByNameRole(currentPage, pageSize, total,queryString);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /*
    * 新增角色
    * */
    @PostMapping
    public Result saveRole(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            this.roleService.saveRole(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /*
    * 删除角色
    * */
    @DeleteMapping(path = "/{id}")
    public Result deleteRole(@PathVariable("id")Integer roleId){
        try {
            this.roleService.deleteRole(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /*
    * 修改角色
    * */
    @PutMapping
    public Result updateUser(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try {
            this.roleService.updateUser(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

}
